// 暴露子仓库的核心配置项
import { login, getUserPro, avatar } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfoList: {}
  },
  mutations: {
    userToken(state, token) {
      state.token = token
      setToken(token)
    },
    userInfo(state, userInfo) {
      state.userInfoList = userInfo
    },
    // 退出清除token
    removeToken(state) {
      state.token = ''
      removeToken()
    },
    removeUserInfo(state) {
      state.userInfoList = {}
    }
  },
  actions: {
    async updatedLogin(context, loginForm) {
      const res = await login(loginForm)
      console.log(res)
      context.commit('userToken', res.data)
    },
    async getUserInfo(context) {
      try {
        const info = await getUserPro() // 获取用户基本信息
        console.log(info, 'info')
        const photo = await avatar(info.data.userId) // 获取用户头像
        console.log(photo, 'photo')
        context.commit('userInfo', { ...info.data, ...photo.data })
      } catch (error) {
        console.log(error)
      }
    },
    logout(context) {
      context.commit('removeToken')
      context.commit('removeUserInfo')
    }
  }
}
